import Taro from "@tarojs/taro";
import { defineStore } from "pinia";
import { computed, ref, watch } from "vue";

import { USER_INFO } from "@/utils/constant";
import { TUserInfo } from "@/global";

const useApp = defineStore("app", () => {
  const userInfo = ref<TUserInfo>(Taro.getStorageSync(USER_INFO));

  // 不能直接使用 userInfo判断是否登录
  const isLogined = computed(() => !!userInfo.value.mobile);
  
  // 增加一个登录中的状态 防止频繁请求
  const isLoginPending = ref(false);

  watch(userInfo, (data) => {
    Taro.setStorageSync(USER_INFO, data);
  });

  return {
    userInfo,
    isLogined,
    isLoginPending,
  };
});

export default useApp;
